<?php

namespace RTSCraft\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use RTSCraft\MainBundle\Entity\Config;
use RTSCraft\MainBundle\Controller\Util\AuthUtil;

class DesignController extends Controller
{
    /**
     * @Route("/design", name="design")
     * @Template()
     */
    public function indexAction()
    {
      // index page
      $is_auth = AuthUtil::is_auth($this);

      if ($is_auth == 0)
	{
	  return $this->render('RTSCraftMainBundle:Admin:design.html.twig');
	}
      else
	{
	  $error = 'Not connected';
	  return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => $is_auth));
	}
    }

    public function changeAction()
    {
      $request = $this->get('request');
      $welcome = '';

      if ($request->getMethod() == 'POST')
      {
          $files = array();
	  $files['logo'] = $request->files->get('logo');
          $files['Rlogo'] = $request->files->get('right');
          $files['bg'] = $request->files->get('bg');
          $welcome = $request->request->get('welcome');
      }
      else
      {
          $error = 'No data submitted.';
          return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => '106'));
      }

      //update config or create new config ?
      $rep = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Config');
      $all = $rep->findAll();
      if (empty($all))
      {
        //nothing in database ?
        $config = new Config();
        $config->setLogo('');
        $config->setRlogo('');
        $config->setBgimg('');
        $config->setWelcomeMsg('Bienvenue sur mon wiki.'); //FIXME.
      }
      else
      {
        //there should be only 1 config in the database ...
        $config = reset($all);
      }

      foreach($files as $index => $file)
      {
          if (($upload = $this->uploadImg($file, $index, $config)) == '\\ERROR//')
          {
             $error = 'Error uploading file';
             $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => '109'));
          }
          else
          {
              switch($index)
              {
                  case 'logo':
                      $config->setLogo($upload);
                      break;

                  case 'Rlogo':
                      $config->setRlogo($upload);
                      break;

                  case 'bg':
                      $config->setBgimg($upload);
                      break;

                  default:
                      break;
              }
          }
      }

      if ($welcome != '')
      {
          $config->setWelcomeMsg($welcome);
      }

      $em = $this->getDoctrine()->getEntityManager();
      $em->persist($config);
      $em->flush();

      $is_auth = (AuthUtil::is_auth($this) == 0 ? true : false);

      return $this->render('RTSCraftMainBundle:Default:mainFrame.html.twig', array('is_auth' => $is_auth));
    }

    private function uploadImg($file, $type, $config)
    {
        if ($file != null)
        {
            //upload !
            $this_ext = $file->guessExtension();
            $authorized_ext = array('png', 'jpg', 'jpeg');

            if (!in_array($this_ext, $authorized_ext))
            {
                return '\\ERROR//';
            }

            $dest_dir = 'uploads';
            $name = $file->getClientOriginalName();
            $file->move($dest_dir, $name);
            return '/uploads/' . $name;
        }
        else
        {
            //no file uploaded, get the last not null upload in the database
            switch ($type)
            {
                case 'logo':
                    return $config->getLogo();
                    break;

                case 'Rlogo':
                    return $config->getRLogo();
                    break;

                case 'bg':
                    return $config->getBgimg();
                    break;

                default:
                    return '';
            }
        }
    }
}



